package cn.kgc.javaweb.service.impl;

import cn.kgc.javaweb.dao.BuildDao;
import cn.kgc.javaweb.dao.impl.BuildDaoImpl;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.Build;
import cn.kgc.javaweb.enums.CodeMsg;
import cn.kgc.javaweb.service.BuildService;
import cn.kgc.javaweb.util.ResponseResult;

import java.util.List;


/**
 * @author: mosin
 * @version: v1.0  2022/11/9
 */
public class BuildServiceImpl implements BuildService {

    private BuildDao buildDao = new BuildDaoImpl();

    @Override//用
    public ResponseResult selectPage(UserPageDTO userPageDTO) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            // 分页查询   code  msg   data  count
            List<Build> builds =  buildDao.selectPage(userPageDTO);
            // 查询数据总条数
            Long  total =  buildDao.selectTotal(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(builds);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用
    public ResponseResult selectById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            Build build =  buildDao.selectById(id);
            responseResult.setData(build);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用
    public ResponseResult update(Build buil,Integer id) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            buildDao.update(buil,id);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override//用
    public ResponseResult add(Build buil) {
        ResponseResult responseResult = ResponseResult.builder().build();
            try {
                buildDao.add(buil);
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            } catch (Exception e) {
                e.printStackTrace();
                responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
                responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            }
        return responseResult;
    }

    @Override//用
    public ResponseResult delete(String ids) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            buildDao.delete(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }
}
